package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.ExamQuestion;
import com.ruoyi.business.domain.UserExamQuestion;
import com.ruoyi.business.domain.bo.ExamQuestionBo;
import com.ruoyi.business.domain.bo.UserExamBo;
import com.ruoyi.business.domain.bo.UserExamQuestionBo;
import com.ruoyi.business.domain.vo.ObjectiveAnswerVo;
import com.ruoyi.business.domain.vo.QuestionVo;
import com.ruoyi.business.mapper.ExamQuestionMapper;
import com.ruoyi.business.mapper.QuestionMapper;
import com.ruoyi.business.mapper.UserExamQuestionMapper;
import com.ruoyi.business.question.QuestionConst;
import com.ruoyi.business.service.IExamQuestionService;
import com.ruoyi.business.service.IQuestionService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 最爱喝兽奶
 * @create 2022/8/12
 **/
@SuppressWarnings("ALL")
@Service
public class ExamQuestionServiceImpl extends ServiceImpl<ExamQuestionMapper, ExamQuestion> implements IExamQuestionService {

    @Autowired
    private ExamQuestionMapper examQuestionMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private UserExamQuestionMapper userExamQuestionMapper;

    @Autowired
    private IQuestionService questionService;

    @Override
    public List<QuestionVo> selectExamQuestionList(ExamQuestionBo bo) {
        // 根据考试信息查询出题目id的列表
        List<String> questionIds = examQuestionMapper.selectQuestionByExam(bo.getExamId(), String.valueOf(bo.getNum()));
        // 根据传入的问题id来查询问题的vo信息
        List<QuestionVo> questionVos = questionMapper.selectQuestionVoByQuestionIds(questionIds);
        List<QuestionVo> list = questionVos.stream().map(item -> {
            if (StringUtils.isNotBlank(item.getAnswerContext())) {{
                // 将问题分割开来
                String[] split = item.getAnswerContext().split(QuestionConst.OBJECTIVE_ANSWER_SEPARATOR);
                List<ObjectiveAnswerVo> answerVos = new ArrayList<>();
                for (String answer : split) {
                    // 答案的属性
                    String[] objAnswerProps = answer.split(QuestionConst.ANSWER_SEPARATOR);
                    ObjectiveAnswerVo vo = new ObjectiveAnswerVo();
                    vo.setSort(objAnswerProps[0]);
                    vo.setContext(objAnswerProps[1]);
                    vo.setId(objAnswerProps[2]);
                    answerVos.add(vo);
                }
                item.setAnswers(answerVos);
            }}
            return item;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<QuestionVo> selectSubjectiveQuestionsByExam(UserExamBo bo) {
        List<String> questionIds = baseMapper.selectSubjectiveQuestionsByExam(bo.getExamId(), String.valueOf(bo.getNum()));
        List<QuestionVo> list = questionService.selectQuestionsByIds(questionIds);
        // 查询这套试卷下的，这套编号下的，所有题型为主观题的学生答题信息
        List<UserExamQuestion> userExamQuestions = userExamQuestionMapper.selectList(new QueryWrapper<UserExamQuestion>()
                .eq("exam_id", bo.getExamId())
                .eq("exam_num", bo.getNum())
                .eq("user_id", bo.getUserId())
                .in("type", Arrays.asList(new String[]{"3", "4"})));
        List<QuestionVo> collect = list.stream().map(item -> {
            for (UserExamQuestion userExamQuestion : userExamQuestions) {
                if (userExamQuestion.getQuestionId().equals(item.getId())) {
                    item.setStuSubAnswer(userExamQuestion.getAnswer());
                    break;
                }
            }
            // 将题目的分值设置成考试题的分值
            QueryWrapper<ExamQuestion> wrapper = new QueryWrapper<>();
            wrapper.eq("exam_id", bo.getExamId());
            wrapper.eq("question_id", item.getId());
            wrapper.eq("num", bo.getNum());
            ExamQuestion examQuestion = examQuestionMapper.selectOne(wrapper);
            item.setScore(String.valueOf(examQuestion.getScore()));
            return item;
        }).collect(Collectors.toList());
        return collect;
    }
}
